#include "led_display.h"

#define SEG_LED_A           0X01        
#define SEG_LED_B           0X02
#define SEG_LED_C           0X04
#define SEG_LED_D           0X08
#define SEG_LED_E           0X10
#define SEG_LED_F           0X20
#define SEG_LED_G           0X40
#define SEG_LED_SPECIAL     0X80

#define LED_DIS_0    (SEG_LED_A + SEG_LED_B + SEG_LED_C + SEG_LED_D + SEG_LED_E + SEG_LED_F )
#define LED_DIS_1    (SEG_LED_B + SEG_LED_C )
#define LED_DIS_2    (SEG_LED_A + SEG_LED_B + SEG_LED_G + SEG_LED_E + SEG_LED_D )
#define LED_DIS_3    (SEG_LED_A + SEG_LED_B + SEG_LED_C + SEG_LED_D + SEG_LED_G )
#define LED_DIS_4    (SEG_LED_B + SEG_LED_F + SEG_LED_G + SEG_LED_C )
#define LED_DIS_5    (SEG_LED_A + SEG_LED_F + SEG_LED_G + SEG_LED_C + SEG_LED_D )
#define LED_DIS_6    (SEG_LED_A + SEG_LED_F + SEG_LED_G + SEG_LED_C + SEG_LED_D + SEG_LED_E )
#define LED_DIS_7    (SEG_LED_A + SEG_LED_B + SEG_LED_C )
#define LED_DIS_8    (SEG_LED_A + SEG_LED_B + SEG_LED_C + SEG_LED_D + SEG_LED_E + SEG_LED_F + SEG_LED_G )
#define LED_DIS_9    (SEG_LED_A + SEG_LED_B + SEG_LED_C + SEG_LED_D + SEG_LED_F + SEG_LED_G )
#define LED_DIS_OFF  (0)

#define LED_DIS_BAT_FRAME   (SEG_LED_A + SEG_LED_B + SEG_LED_C + SEG_LED_D)
#define LED_DIS_MASSAGE     (SEG_LED_A + SEG_LED_B + SEG_LED_C)
#define LED_DIS_EXPRESSION  (SEG_LED_D + SEG_LED_E + SEG_LED_F)
#define LED_DIS_STOP        (SEG_LED_A + SEG_LED_B + SEG_LED_C)

static uint8_t led_dis_bat_buffer[4] = {
                                        (LED_DIS_BAT_FRAME),
                                        (LED_DIS_BAT_FRAME + SEG_LED_G),
                                        (LED_DIS_BAT_FRAME + SEG_LED_F + SEG_LED_G),
                                        (LED_DIS_BAT_FRAME + SEG_LED_E + SEG_LED_F + SEG_LED_G)};

static uint8_t led_dis_symbol_buffer[2] = {
                                            (LED_DIS_EXPRESSION),
                                            (LED_DIS_MASSAGE)};

static uint8_t led_dis_buffer[12] = {
                                    LED_DIS_0,
                                    LED_DIS_1,
                                    LED_DIS_2,
                                    LED_DIS_3,
                                    LED_DIS_4,
                                    LED_DIS_5,
                                    LED_DIS_6,
                                    LED_DIS_7,
                                    LED_DIS_8,
                                    LED_DIS_9,
                                    SEG_LED_SPECIAL,
                                    LED_DIS_OFF};
static uint8_t led_dis_time_buff = 0;
void led_display_init(void)
{
    disp_init();
}
/*刷新数字区两个地址*/
void led_display_set(seg_addr_t t_addr,uint8_t led_dis_buff)
{
    uint8_t addr = t_addr;
    for(uint8_t i=0;i<2;i++)
    {
        I2CStart();
        I2CWritebyte(addr);	
        I2CWritebyte(led_dis_buff);
        I2CStop();
        addr++;
    }
}
/*刷新地址*/
void led_display_set_rest(seg_addr_t t_addr,uint8_t index)
{
    uint8_t addr = t_addr;

    I2CStart();
    I2CWritebyte(addr);	
    I2CWritebyte(index);
    I2CStop();
}
/*全显*/
void led_display_on_all(void)
{
    uint8_t addr = TYPE_TIM_TENS_L;
    for(uint8_t i=0;i<13;i++)
    {
        
        I2CStart();
        I2CWritebyte(addr);	
        I2CWritebyte(0xff);
        I2CStop();
        addr++;
    }
    //关没有用到的显示20240723
    I2CStart();
    I2CWritebyte(TYPE_TIM_TENS_L+0x0C);	
    I2CWritebyte(0);
    I2CStop();
    I2CStart();
    I2CWritebyte(TYPE_TIM_TENS_L+0x08);	
    I2CWritebyte(0);
    I2CStop();

}

/*全灭*/
void led_display_off_all(void)
{
    uint8_t addr = TYPE_TIM_TENS_L;
    for(uint8_t i=0;i<13;i++)
    {
        I2CStart();
        I2CWritebyte(addr);	
        I2CWritebyte(0);
        I2CStop();
        addr++;
    }
}
/*控制LED TM1640 的亮度 设置脉冲宽度 最大值7，最小值0*/
void led_display_LEVEL(uint8_t DIS_LEVEL)
{
    I2CStart();
    I2CWritebyte(DIS_LEVEL + 0x88);	
    I2CStop();
}
/*显示时间十位*/
void led_display_tens_digit(uint8_t tens_set)
{
    led_dis_time_buff &= SEG_LED_SPECIAL;
    led_dis_time_buff |= led_dis_buffer[tens_set];
    led_display_set(TYPE_TIM_TENS_L,led_dis_time_buff);
}
/*关闭时间的十位*/
void led_display_tens_off(void)
{
  led_dis_time_buff &= SEG_LED_SPECIAL;
    led_display_set(TYPE_TIM_TENS_L,led_dis_time_buff);
}
/*显示时间的个位*/
void led_display_units_digit(uint8_t units)
{
   led_display_set(TYPE_TIM_UNITS_L,led_dis_buffer[units]);
}
/*关闭时间的个位*/
void led_display_units_off(void)
{
    led_display_set(TYPE_TIM_UNITS_L,led_dis_buffer[10]);
}
/*关闭时间的显示*/
void led_display_tim_seg_off(void)
{
    led_display_tens_off();
    led_display_units_off();
}
/*显示档位*/
void led_display_shift(uint8_t shift_set)
{ 
   led_display_set(TYPE_VACUUM_UNITS_L,led_dis_buffer[shift_set]);
}
/*关闭档位*/
void led_display_shift_off(void)
{ 
   led_display_set(TYPE_VACUUM_UNITS_L,led_dis_buffer[11]);
}
/*关闭档位*/
void led_display_shift_seg_off(void)
{
    led_display_shift_off();
}
/*关闭时间显示，仅显示时间图标*/
void led_display_tim_on(void)
{ 
   led_display_set(TYPE_TIM_TENS_L,led_dis_buffer[10]);
}
/*关闭时间显示和时间图标*/
void led_display_tim_off(void)
{ 
   led_display_set(TYPE_TIM_TENS_L,led_dis_buffer[11]);
}
/*关闭档位显示，仅显示档位图标*/
void led_display_vacuum_on(void)
{ 
   led_display_set(TYPE_VACUUM_TENS_L,led_dis_buffer[10]);
}
/*打开时间图标*/
void led_dispialy_timIcon_on(void)
{
    led_dis_time_buff |= SEG_LED_SPECIAL;
    led_display_set(TYPE_TIM_TENS_L,led_dis_time_buff);
}
/*关闭时间图标*/
void led_dispialy_timIcon_off(void)
{
    led_dis_time_buff &= (~SEG_LED_SPECIAL);
    led_display_set(TYPE_TIM_TENS_L,led_dis_time_buff);
}

/*电量显示*/
void led_display_bat(uint8_t bat_level)
{
    led_display_set_rest(TYPE_BAT,led_dis_bat_buffer[bat_level]);
}
/*模式显示*/
void led_display_symbol(uint8_t symbol)
{
    led_display_set_rest(TYPE_EXPRESSION,led_dis_symbol_buffer[symbol]);
}
/*显示暂停图标*/
void led_display_stop_on(void)
{
    led_display_set_rest(TYPE_STOP,LED_DIS_STOP);
}
/*关闭暂停图标*/
void led_display_stop_off(void)
{
    led_display_set_rest(TYPE_STOP,0);
}

void led_display_init_show(
                            uint8_t mode,
                            uint8_t s_gear,
                            uint8_t h_gear,
                            uint8_t *bat,
                            uint8_t task_tim)
{
    uint8_t gear = 0;
    uint8_t tens,units;
     
    *bat = bat_led_gear(get_bat_volt());
    led_display_vacuum_on();
    if(mode) 
    {
        gear = s_gear;
    } 
    else 
    {
        gear = h_gear;
    }
    led_display_symbol(mode);
    led_display_shift(gear + 1);
    led_display_bat(*bat);
    tens= (uint8_t)task_tim / 10;
    units= (uint8_t)task_tim % 10;
    led_display_tens_digit(tens); 
    led_display_units_digit(units);
}